﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using Charities.ModelViews;
using System.IO;

namespace Charities.Forms
{
    public partial class GreantsReportsAll : Form
    {
        int userid;
        public bool is_loaded=false;
        Form fo = null;
        private bool dtp_fdate { get; set; }
        private bool dtp_tdate { get; set; }
        ModelContainer ctx = new ModelContainer();
        public GreantsReportsAll(int uid,Form fm)
        {
            fo = fm;
            MdiParent = fm;
            userid = uid;
            InitializeComponent();
        }

        private void GreantsReportsAll_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
        }

        private void GreantsReportsAll_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void GreantsReportsAll_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

        private void btn_search_Click(object sender, EventArgs e)
        {

            var allGrantDateials = ctx.GrantDetails.ToList();
            if (dtp_fdate)
            {
                var fromGDetails = allGrantDateials.
                    Where(c => c.InsertDate.Value.Year >= dtp_FromDate.Value.Year
                    && c.InsertDate.Value.Month >= dtp_FromDate.Value.Month
                    && c.InsertDate.Value.Day >= dtp_FromDate.Value.Day).ToList();
                allGrantDateials = fromGDetails;

            }
            if (dtp_tdate)
            {
                var toGDetails = allGrantDateials.
                   Where(c => c.InsertDate.Value.Year <= dtp_FromDate.Value.Year
                && c.InsertDate.Value.Month <= dtp_FromDate.Value.Month
                && c.InsertDate.Value.Day <= dtp_FromDate.Value.Day).ToList();
                allGrantDateials = toGDetails;
            }
            List<ModelViews.GrantReportView> GView = new List<ModelViews.GrantReportView>();
            foreach (var grantd in allGrantDateials)
            {
                if (grantd.Grant != null)
                {
                    Person p = ctx.People.Single(c => c.Id == grantd.Grant.Worthy.PersonId);
                    if (grantd.SupplyId != null)
                    {
                        ModelViews.GrantReportView gvw = new ModelViews.GrantReportView();
                        gvw.DisplayName=string.Format("{0} {1} {2}",p.FirstName,p.FatherName,p.LastName);
                        gvw.IssueDate = grantd.Grant.IssueDate;
                        gvw.IssueNumber = grantd.Grant.IssueNumber;
                        if (grantd.Grant.Schedules.Count > 0)
                            gvw.GrantDetail = " (مجدولة) " + grantd.Supply.Name + " بتاريخ " + grantd.Grant.Schedules.Single(c => c.GrantId == grantd.GrantId).NextGrantDate.ToShortDateString();
                        else if (grantd.Grant.IsDelivered == true)
                            gvw.GrantDetail = " (مسلمة) " + grantd.Supply.Name;
                        else if (grantd.Grant.IsDelivered != true)
                            gvw.GrantDetail = "  (غير مسلمة) " + grantd.Supply.Name;
                        else
                            gvw.GrantDetail = grantd.Supply.Name;
                        gvw.Ammount = grantd.Amount;
                        gvw.insertDate = grantd.Grant.DateOfDelivery.Value;
                        gvw.FromDate = dtp_FromDate.Value;
                        gvw.ToDate = dtp_toDate.Value;
                        GView.Add(gvw);
                    }
                    else
                    {
                        ModelViews.GrantReportView gv = new ModelViews.GrantReportView();
                        gv.DisplayName = string.Format("{0} {1} {2}", p.FirstName, p.FatherName, p.LastName);
                        gv.IssueDate = grantd.Grant.IssueDate;
                        gv.IssueNumber = grantd.Grant.IssueNumber;
                        if (grantd.Grant.Schedules.Count() > 0)
                            gv.GrantDetail = " (مجدولة) " + grantd.Grant.Cart.Name + " بتاريخ " + grantd.Grant.Schedules.Single(c => c.GrantId == grantd.Grant.Id).NextGrantDate.ToShortDateString();
                        else if (grantd.Grant.IsDelivered == true)
                            gv.GrantDetail = " (مسلمة) " + grantd.Grant.Cart.Name;
                        else if (grantd.Grant.IsDelivered != true)
                            gv.GrantDetail = " (غير مسلمة) " + grantd.Grant.Cart.Name;
                        else
                            gv.GrantDetail = grantd.Grant.Cart.Name;
                        gv.Ammount = grantd.Amount;
                        gv.DileveryDate = grantd.Grant.DateOfDelivery.Value;
                        gv.FromDate = dtp_FromDate.Value;
                        gv.ToDate = dtp_toDate.Value;
                        GView.Add(gv);
                    }
                }
            }
            GrantReportViewBindingSource.DataSource = GView;
            reportViewer1.LocalReport.EnableExternalImages = true;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.reportViewer1.RefreshReport();
            dtp_fdate = false;
            dtp_tdate = false;

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void dtp_FromDate_Validated(object sender, EventArgs e)
        {
            dtp_fdate = true;
        }

        private void dtp_toDate_Validated(object sender, EventArgs e)
        {
            dtp_tdate = true;
        }
    }
}
